package org.kukker.view;


import org.kukker.spinner.KukkerSpinner;
import org.kukker.spinner.KukkerSpinnerAdapter;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import org.kukker.R;
import org.kukker.Util;
import org.kukker.event.Event;
import org.kukker.event.EventSearchParameters;


import android.content.Context;
import android.graphics.Rect;
import android.location.LocationManager;
import android.text.Editable;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class SearchEventsInput extends LinearLayout{
	
	private EditText mSearchText;
	private EditText mLocation;
	private Button  mAutoDetectBtn;
	private Button mSearchBtn;
	
	private Properties mEventSearchParameters;
	
	public SearchEventsInput(Context context){
		super(context);		
		this.mContext = context;
	}
	
	public SearchEventsInput(Context context, AttributeSet attrs, Map inflateParams) {
        super(context, attrs, inflateParams);
        this.mContext = context;
	}
	
	protected void onFinishInflate(){
		this.init();
	}
	
	private void init(){
		this.mEventSearchParameters = new Properties();
		TextView tv = new TextView(this.mContext);
		
		mAutoDetectBtn = (Button) findViewById(R.id.eventsearchinput_button_autodetectlocation);
		mSearchBtn = (Button) findViewById(R.id.eventsearchinput_button_search);
		
		mLocation = (EditText) findViewById(R.id.eventsearchinput_edittext_location);
		mSearchText = (EditText) findViewById(R.id.eventsearchinput_edittext_searchterm);
		
		
		mAutoDetectBtn.setOnFocusChangeListener(new OnFocusChangeListener(){

			@Override
			public void onFocusChanged(View arg0, boolean gainFocus) {
				if(gainFocus){
				//	mLocation.setEnabled(false);					
				}else{
					//mLocation.setEnabled(true);
				}
				
			}
			
		});
		
		//get location service
		//LocationManager locationService = this.mContext.getSystemService(Context.LOCATION_SERVICE);
		
		//wire up events
		mAutoDetectBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		mSearchBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if(mSearchEventsInputListener != null){			
					mSearchEventsInputListener.OnSearchClicked(getSearchParameters());
				}				
			}			
		});
		
	}
	
	private ISearchEventsInputListener mSearchEventsInputListener;
	public void setSearchEventsInputListener(ISearchEventsInputListener ls){
		this.mSearchEventsInputListener = ls;
	}
	public Properties getSearchParameters(){
		Editable e1 = this.mLocation.getText();
		Editable e2 =this.mSearchText.getText();
				
		String location = "";
		String searchTerm = "";
		
		if(e1 != null){
			location = e1.toString();	
		}else{
			location = "";
		}
		if(e2 != null){
			searchTerm = e2.toString();	
		}else{
			searchTerm = "";
			
		}
		location = location.trim();
		searchTerm = searchTerm.trim();
		
		this.mEventSearchParameters.put("location",location);
		this.mEventSearchParameters.put("search_text",searchTerm);
		
		return this.mEventSearchParameters;
	}
	
	private void initLookAndFeel(){
		
	}
	public boolean onTouchEvent(MotionEvent event){
		
		super.onTouchEvent(event);//important
		
		
		 String actionName = org.kukker.Util.kMotionEvent.getActionName(event);
		 Log.i("eventflow1",
   			"["+this.getClass().getSimpleName()+"]" +
   			
   			"["+
   			"isSelected:"+this.isSelected()+
   			", focused:"+this.isFocused() +
   			"]"
   			
   			+"["
   			+"onTouchEvent()"    			
   		
   			+", action:"+actionName
   			
   			+"]"
   			
		 );
		 
		 //this.setSelected(true);
		 return true;
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event){		
		boolean res = super.onKeyDown(keyCode, event); //important
		
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			", isFocused:"+this.isFocused()+
    			"]"
    			
    			+"["
    			+"onKeyDown() " +
    			",keyCode:"+keyCode
    			
    			+"]"    			
    	);
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
			
			
			//since we want to detect on key up later we have to stop this event from propagating.
			//stop propagating 
			return true;
		}
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			return true;
		}
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){	
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			return true;		
		}else{
			
			
					
			View v = this.findFocus();
			if(v!=null){
				Log.i("debug",this.getClass().getSimpleName()+", found focus: "+v.getClass().getSimpleName());
				v.requestFocus();
			}else{				
				View v2 = this.focusSearch(View.FOCUS_DOWN);
				if(v2 != null){
					Log.i("debug",this.getClass().getSimpleName()+", found given to: "+v2.getClass().getSimpleName());
				}
			}
			
		}
		
		return res; 
		
    	
	}
	
	

	public boolean onKeyUp(int keyCode, KeyEvent event){		
		boolean res = super.onKeyUp(keyCode, event); //important
		
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			", isFocused:"+this.isFocused()+
    			"]"
    			
    			+"["
    			+"onKeyUp() " +
    			",keyCode:"+keyCode
    			
    			+"]"    			
    	);
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
			
			
			//since we want to detect on key up later we have to stop this event from propagating.
			//stop propagating 
			return true;
		}
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			

			//mEventDetailViewListener.onKeyRightUp();			
			
			return true;
		}
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){	
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			return true;		
		}
		
		return res;    	
	}
	
	public void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
		Log.i("eventflow1",

		"[" + this.getClass().getSimpleName() + "]" +

		"[" + " isSelected:" + this.isSelected() 
			+ ", isFocused:" + this.isFocused()
		+ "]"

		+ "[" + "onFocusChanged()" + ", gainFocus:" + gainFocus
				+ ", direction:" + direction + "]"

		);		
		
		if (gainFocus) {
			//this.setFocusable(false);
			/*
			boolean focusTaken = this.requestFocus(View.FOCUS_DOWN);
						
			//View whoHasFocus = this.findFocus();
			
			View aView = this.focusSearch(View.FOCUS_DOWN);
			
			if(aView != null){
				aView.requestFocus();
				aView.setSelected(true);
				//EventView view2 = (EventView)whoHasFocus;			
				Log.i("eventflow1",this.getClass().getSimpleName()+", focus is passed to :" + aView.toString()
				+", focusTaken:"+focusTaken		
				);
						//+", position:"+view2.getPosition());
			}
			
			 
			if(!focusTaken){
				//give focus to an edit text
				this.mLocation.requestFocus();
			}*/
			
			//this.setFocusable(false);
			//this.clearFocus();
			
			//this.mLocation.setEnabled(true);
			//this.mLocation.setSelected(true);
			//this.mLocation.setFocusable(true);
			//this.mLocation.setClickable(true);
			
			this.setFocusable(false);
			boolean f = this.requestFocus(View.FOCUS_RIGHT);
			Log.i("debug","focus found:"+f);
			
		
		} else {
			
			//this.setFocusable(false);
		}
		

	}
	
	
	
	public void setDetectIncomingEvents(boolean state){
		
			//
			this.setClickable(state);
			this.setFocusable(state);
			this.setFocusableInTouchMode(state);
			//this.setBlockChildEvents(!state);
			this.setBlockDescendantFocus(!state);//blocks or ignoring focus from a key down
			this.setEnabled(state);
			//
			if(!state){
				View v = this.findFocus();
				if(v!=null){v.clearFocus();}
			}
	}
	
	
}